package com.qf.smartplatform.service.impl;

import com.qf.smartplatform.cache.CategoryCache;
import com.qf.smartplatform.dto.Result;
import com.qf.smartplatform.events.CategoryChangeEvent;
import com.qf.smartplatform.exceptions.AddDataException;
import com.qf.smartplatform.exceptions.DeleteDataException;
import com.qf.smartplatform.exceptions.QueryDataException;
import com.qf.smartplatform.exceptions.ResultCode;
import com.qf.smartplatform.mapper.CategoryMapper;
import com.qf.smartplatform.pojo.Category;
import com.qf.smartplatform.pojo.CheckType;
import com.qf.smartplatform.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import javax.security.auth.login.AppConfigurationEntry;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * projectName:day12_27_Test
 * author:guoLei
 * time:2021/12/29 20:15
 * description:
 */
@Service
@Transactional
public class CategoryServiceImpl implements CategoryService {

    private ApplicationContext context;

    @Autowired
    public void setContext(ApplicationContext context) {
        this.context = context;
    }

    private CategoryMapper categoryMapper;

    @Autowired
    public void setCategoryMapper(CategoryMapper categoryMapper) {
        this.categoryMapper = categoryMapper;
    }
    private CategoryCache categoryCache;
    @Autowired
    public void setCategoryCache(CategoryCache categoryCache) {
        this.categoryCache = categoryCache;
    }

    @Override
    public void addCategory(Category category) {
        Assert.isTrue(!category.isEmpty(CheckType.ADD),()->{
            throw new AddDataException(" 必须传递的数据不完整，请检查", ResultCode.USERNAME_NULL);
        });

        Category currentCategory=categoryMapper.selectByName(category.getCategoryName());
        Assert.isNull(currentCategory,()->{
            throw new AddDataException("分类已经存在",ResultCode.DATA_ALREADY_EXIST);
        });
        category.setCreateTime(new Date());
        context.publishEvent(new CategoryChangeEvent());
    }

    @Override
    public int updateCategory(Category category) {
        return 0;
    }

    @Override
    public Category selectById(Long cId) {
        Assert.isTrue(cId>0,()->{
            throw new QueryDataException("主键超出允许的范围", ResultCode.ID_NOT_ALLOWED);
        });

        Category category = categoryCache.getValue(cId);
        return category;
    }

    @Override
    public List<Category> selectAllCategories() {
        List<Category>categories=categoryCache.getAllData();
        return categories;
    }

    @Override
    public List<Category> selectCategoriesByNameLike(String cName) {
        Assert.hasText(cName,()->{
            throw new QueryDataException("分类名为空",ResultCode.QUERY_PARAM_EMPTY);
        });
        List<Category> categories=categoryCache.getAllData();
        List<Category>list=categories.stream().filter(category -> category.getCategoryName().contains(cName))
                .collect(Collectors.toList());
        return list;
    }

    @Override
    public int deleteCategoriesByIds(List<Long> ids) {
    Assert.notEmpty(ids,()->{
        throw new DeleteDataException("没有传递id",ResultCode.ID_NULL);
    });
    int result=categoryMapper.deleteCategoriesByIds(ids);
    context.publishEvent(new CategoryChangeEvent());
    return result;
    }
}
